/**
 * ����6:53:33
 * @author
 */
package org.jsoft.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.jsoft.common.DBConnection;
import org.jsoft.common.DateConverter;
import org.jsoft.inter.LogInter;
import org.jsoft.vo.Emp;
import org.jsoft.vo.Log;

/**
 * ��־����
 * @author �Ǳ�
 *
 */
public class LogDao implements LogInter {


	@Override
	public boolean addLog(Log log) {
		String add = "insert into log(log_title,log_content,log_commit_time,emp_id) values('"+log.getLogTitle()+"','"+log.getLogContent()+"','"+log.getLogCommitTime()+"',"+log.getEmpId()+")";
		if(modify(add)){
			return true;
		}
		return false;
	}


	@Override
	public boolean delLog(int logId) {
		String del = "delete from log where log_id='"+logId+"'";
		if(modify(del)){
			return true;
		}
		return false;
	}


	@Override
	public boolean alterLog(Log log) {
		String update = "";
		if(!("".equals(log.getLogContent()))){
			update = "update log set log_content='"+log.getLogContent()+"' where log_id="+log.getLogId();
			modify(update);
		}
		if(!("".equals(log.getLogTitle()))){
			update = "update log set log_title='"+log.getLogTitle()+"' where log_id='"+log.getLogId()+"'";
			modify(update);
		}
		return false;
	}


	@Override
	public Log queryLog(int logId) {
		String query = "select log_id,log_content,log_title,log_commit_time,emp_id from log where log_id='"+logId+"'";
		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		Log log = null;
		
		conn = DBConnection.getInstance().getConnection();
		try {
			st = conn.createStatement();
			rs = st.executeQuery(query);
			
			while(rs.next()){
				log = new Log();
				log.setLogId(rs.getInt("Log_id"));
				log.setLogContent(rs.getString("log_content"));
				log.setLogTitle(rs.getString("log_title"));
				log.setLogCommitTime(rs.getDate("log_commit_time").toString());
				log.setEmpId(rs.getInt("emp_id"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
		return log;
	}
	
	
	public List<Log> queryByempId(Emp emp){
		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		List<Log> logs = new ArrayList<Log>();
		String query = "select log_id,log_content,log_title,log_commit_time,emp_id from log where emp_id='"+emp.getEmpId()+"'";
		conn = DBConnection.getInstance().getConnection();
		try {
			st = conn.createStatement();
			rs = st.executeQuery(query);
			while(rs.next()){
				Log log = new Log();
				log.setLogId(rs.getInt("Log_id"));
				log.setLogContent(rs.getString("log_content"));
				log.setLogTitle(rs.getString("log_title"));
				log.setLogCommitTime(rs.getDate("log_commit_time").toString());
				log.setEmpId(rs.getInt("emp_id"));
				logs.add(log);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
		return logs;
	}

	@Override
	public List<Log> queryAllLog() {
		String query = "select log_id,log_content,log_title,log_commit_time,emp_id from log";
		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		List<Log> logs = new ArrayList<Log>();
		conn = DBConnection.getInstance().getConnection();
		
		try {
			st = conn.createStatement();
			rs = st.executeQuery(query);
			while(rs.next()){
				Log log = new Log();
				log.setLogId(rs.getInt("Log_id"));
				log.setLogContent(rs.getString("log_content"));
				log.setLogTitle(rs.getString("log_title"));
				log.setLogCommitTime(DateConverter.dateToString(rs.getTimestamp("log_commit_time")));
				log.setEmpId(rs.getInt("emp_id"));
				logs.add(log);
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		return logs;
	
	}
	public boolean modify(String sql){
		boolean flag = true;
		Connection conn = null;
		Statement statement = null;
		try {
			conn = DBConnection.getInstance().getConnection();
			statement = conn.createStatement();
			statement.executeUpdate(sql);
		} catch (SQLException e) {
			flag = false;
		}finally{
			DBConnection.getInstance().close(statement, conn);
		}
		return flag;
		
	}
	
	
}
